package vip.meet.util;

import java.util.Random;

public final class DataGenerateUtil {

    private DataGenerateUtil() {
        throw new UnsupportedOperationException("工具类,不用初始化");
    }

    /**
     * 获取n位数字(0-9) number,digit
     * 
     * @param n
     * @return
     */
    public static String getDigit(int n) {
        Random random = new Random();
        return getDigit(n,random);
    }
    
    public static String getDigit(int n,Random random) {
        StringBuffer sb = new StringBuffer(n);
        for (int i = 0; i < n; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 获取n位小写字母(a-z) alphabet
     * 
     * @param n
     * @return
     */
    public static String getLowerAlphabet(int n) {
        return getAlphabet(n, true);
    }

    /**
     * 获取n位大写字母(A-Z)
     * 
     * @param n
     * @return
     */
    public static String getUpperAlphabet(int n) {
        return getAlphabet(n, false);
    }

    /**
     * 获取n为字母(a-zA-Z)
     * @param n
     * @return
     */
    public static String getAlphabet(int n) {
        StringBuffer sb = new StringBuffer(n);
        Random random = new Random();
        boolean lower = random.nextBoolean();
        for (int i = 0; i < n; i++)
            sb.append(getChar(random, lower));
        return sb.toString();
    }
    
    /**
     * 获取n为字母或数字[0-9a-zA-Z]
     * @param n
     * @return
     */
    public static String getAlnum(int n){
        StringBuffer sb = new StringBuffer(n);
        Random random = new Random();
        char[] alnumCharArray = getAlnumCharArray();
        int length = alnumCharArray.length;
        for(int i=0;i<n;i++)
            sb.append(alnumCharArray[random.nextInt(length)]);
        return sb.toString();
    }
    
    public static char [] getAlnumCharArray(){
        char [] alnum = new char[62];
        int index = 0;
        for(char i = '0';i<='9';i++)
            alnum[index++] = i;
        for(char i = 'a';i<='z';i++)
            alnum[index++] = i;
        for(char i = 'A';i<='Z';i++)
            alnum[index++] = i;
       return alnum;
    }
    
    /**
     * 生成手机号码
     * @return
     */
    public static String getPhone(){
        Random random = new Random();
        String phonePrefix = getPhonePrefix(random);
        String digit = getDigit(8,random);
        return phonePrefix + digit;
    }
    
    private static String getAlphabet(int n, boolean lower) {
        StringBuffer sb = new StringBuffer(n);
        Random random = new Random();
        for (int i = 0; i < n; i++)
            sb.append(getChar(random, lower));
        return sb.toString();
    }

    private static char getChar(Random random, boolean lower) {
        char base;
        if (lower)
            base = 'a';
        else
            base = 'A';
        int offset = random.nextInt(26);
        char c = (char) (base + offset);
        return c;
    }
    
    
    /**
     * 获取手机号前缀数组
     * @return
     */
    public static String [] phonePrefixArray(){
        String prefix = "130,131,132,133,134,135,136,137,138,139,145,147,150,151,152,153,155,156,157,158,159,170,173,175,176,177,178,180,181,182,183,184,185,186,187,188,189";
        return prefix.split(",");
    }
    
    /**
     * 获取手机号码前缀
     * @return
     */
    public static String getPhonePrefix(){
        Random random = new Random();
        return getPhonePrefix(random);
    }
    
    public static String getPhonePrefix(Random random){
        String [] phoneArray = {"130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "145", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159", "170", "173", "175", "176", "177", "178", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189"};
        int length = phoneArray.length;
        return phoneArray[random.nextInt(length)];
    }

}
